var _store;
var _sort;
Ext.onReady(function(){
		var _columnModle = new Ext.grid.ColumnModel({
	 		columns:[
	 		{
	 			header: "环节",sortable : true,renderer:function(){
	 			     var str = Ext.get("description").getValue();
	 			     return str;
	 			}
	 		},{
	 			header: "常用语", menuDisabled: false,dataIndex:'sentence'
 			},
	 		{
	 			header: "操作", menuDisabled: false,dataIndex:'handler',renderer:function(s,m,rec){
	 				var str = "<a href=\"javaScript:updateDefalt('"+rec.data.id+"','"+rec.data.sentence+"') \">编辑</a>"+
	 				"&nbsp;&nbsp;<a href=\"javaScript:deleteChangyongyu('"+rec.data.id+"') \">删除</a>";
	 				return str;
	 			}
	 		}
	 		]
	 	});
	 	
					
		_store = new Ext.data.JsonStore({
	 		fields:['id','mainId','sentence','handler'],
	 		url: Ext.get('basePath').getValue()+'tjsinfo/tjsoa/system/changyong.do?method=queryAllChangYongYuDefalt&mainId='+Ext.get("changyongyuid").getValue(),
	 		root: 'root',
            totalProperty: 'totalProperty'
	 	});
	 	

		 	
	var bbar = new Ext.PagingToolbar({
		pageSize:10,
		store:_store,

		   //显示右下角信息		   
		   displayInfo:true,
		   displayMsg:'当前记录 {0} -- {1} 条 共 {2} 条记录',
	       prevText:"上一页",
		   nextText:"下一页",
		   refreshText:"刷新",
		   lastText:"最后页",
		   firstText:"第一页",
		   beforePageText:"当前页",
		   afterPageText:"共{0}页",
		   emptyMsg:'没有记录',
		   items:[
                '-','每页显示行数:',
                 {  name:'txtpagesize',
                	id:'txtpagesize',
                	xtype:'numberfield',
					emptyText:10,
					width:30          
                },{
                	text: '确定',
			            handler: function(){
			                bbar.pageSize = parseInt(Ext.getCmp('txtpagesize').getValue());
					        _store.reload({params:{start:0,limit:bbar.pageSize}}); 
			                }
            }]
	});
	
	 	var _toolbar;
	 	
	 	 _toolbar = new Ext.Toolbar({
	         items: [
	           {
	               id: 'buttonA',
				   iconCls: 'add',
	               text: "新建",
	               handler: function() {
						 Ext.MessageBox.prompt("默认常用语新增","请输入常用语",function(bnt,text){
							if(bnt=="ok"){
							    if(text.length>20){
							         Ext.MessageBox.show({ title:Ext.systemTitle,
										   msg: '常用语字数不能大于20！',
										   buttons: Ext.Msg.OK,						  					
										   icon: Ext.MessageBox.INFO
										});
							    }else if(text == ""){
							    	  Ext.MessageBox.show({ title:Ext.systemTitle,
										   msg: '常用语不能为空！',
										   buttons: Ext.Msg.OK,						  					
										   icon: Ext.MessageBox.INFO
										});
							    }else{
								Ext.Ajax.request({
									url:Ext.get('basePath').getValue()+'tjsinfo/tjsoa/system/changyong.do?method=addDefalt',
								    params: { mainId: Ext.get("changyongyuid").getValue(),changyongyu:text},
								    success:function(){
								    	Ext.MessageBox.show({ title:Ext.systemTitle,
										   msg: '添加成功！',
										   buttons: Ext.Msg.OK,						  					
										   icon: Ext.MessageBox.INFO
										});
								    	 _store.reload();
								    },
								    failure:function(){
								    	Ext.MessageBox.show({ title:Ext.systemTitle,
										   msg: '添加失败！',
										   buttons: Ext.Msg.OK,						  					
										   icon: Ext.MessageBox.INFO
										});
								    }
								});}
							}
						});
        
	               }
				}]
			});
	

		var _table = new Ext.grid.GridPanel({
			colModel:_columnModle,
			sm: new Ext.grid.RowSelectionModel({singleSelect:true}),
	 		store:_store,
		    width: Ext.get("showRegional").getWidth(),
		    height:Ext.get("showRegional").getHeight(),
		    title: _title+"-"+Ext.get("description").getValue(),
		    renderTo:Ext.get('showRegional'),
		    frame: true,
		    viewConfig:{
		    	forceFit: true
		    },
		    clicksToEdit:1, 
		    layout : 'fit',// 表格内列内容填充满,按列头比例填充 
		    loadMask:{ 
			msg : '数据加载中，请稍后...' 
			}, 
		    stripeRows:true,
		    tbar: _toolbar,
		    bbar:bbar


		});
		_store.load({params:{start:0, limit:10}});	
});

/**
 * 删除
 */
function deleteChangyongyu(id,mainId){
	Ext.MessageBox.confirm(Ext.systemTitle, '是否删除此常用语?', function(btn){
		if(btn=='yes'){
			Ext.a
			Ext.Ajax.request({
				url:Ext.get('basePath').getValue()+'tjsinfo/tjsoa/system/changyong.do?method=deleteDefalt',
			    params: { id: id},
			    success:function(){
			    	Ext.MessageBox.show({ title:Ext.systemTitle,
					   msg: '删除成功！',
					   buttons: Ext.Msg.OK,						  					
					   icon: Ext.MessageBox.INFO
					});
			    	 _store.reload();
			    },
			    failure:function(){
			    	Ext.MessageBox.show({ title:Ext.systemTitle,
					   msg: '删除失败！',
					   buttons: Ext.Msg.OK,						  					
					   icon: Ext.MessageBox.INFO
					});
			    }
			});	
		}
	});
}

/**
 * 更新常用语
 */
function updateDefalt(id,sentence){
	Ext.MessageBox.prompt("默认常用语修改","请输入常用语",function(bnt,text){
		if(bnt=="ok"){
		    if(text.length>20){
		        Ext.MessageBox.show({ title:Ext.systemTitle,
					   msg: '常用语字数不能大于20！',
					   buttons: Ext.Msg.OK,						  					
					   icon: Ext.MessageBox.INFO
				});
		    }else if(text == ""){
		    	Ext.MessageBox.show({ title:Ext.systemTitle,
					   msg: '常用语不能为空！',
					   buttons: Ext.Msg.OK,						  					
					   icon: Ext.MessageBox.INFO
				});
		    }else{
			Ext.Ajax.request({
				url:Ext.get('basePath').getValue()+'tjsinfo/tjsoa/system/changyong.do?method=updateDefalt',
			    params: { id: id,changyongyu:text},
			    success:function(){
			    	Ext.MessageBox.show({ title:Ext.systemTitle,
					   msg: '更新成功！',
					   buttons: Ext.Msg.OK,						  					
					   icon: Ext.MessageBox.INFO
					});
			    	 _store.reload();
			    },
			    failure:function(){
			    	Ext.MessageBox.show({ title:Ext.systemTitle,
					   msg: '更新失败！',
					   buttons: Ext.Msg.OK,						  					
					   icon: Ext.MessageBox.INFO
					});
			    }
			});}
		}
	},this,false,sentence);
}


 function windowonload(){
         if (_sessionUser==null){
    	    Ext.MessageBox.alert('提示', '请先登录',clickLink);     
         }
        }
        
function clickLink(){
		window.location.href='';
	}		
		